home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
oscar
/
OscarUtil.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
15KB
|
432 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from util.primitives import odict
import struct
import util
import sys
import array
import logging
import string
from collections import defaultdict
import oscar
log = logging.getLogger('oscar.util')
len_to_fmt = {
0: '',
1: 'B',
2: 'H',
4: 'I',
8: 'Q',
16: '16s' }
def lowerstrip(s):
return s.lower().replace(' ', '')
def tflv(type_, flags = 1, data = ''):
return struct.pack('!HBB', type_, flags, len(data)) + data
def tflv_list(*tflvs):
return ''.join((lambda .0: for t in .0:
tflv(*t))(tflvs))
def tlv(type_, length = 0, value = 0):
if isinstance(length, basestring):
value = length
length = len(value)
elif not isinstance(value, (int, long)):
raise AssertionError('%r is not an integer' % value)
if isinstance(value, basestring):
fmt_string = None if value else ''
else:
fmt_string = len_to_fmt[length]
args = [
type_]
None += args if length else [
0]
if value:
pass
elif length:
pass
args += []
try:
return struct.pack('!HH' + fmt_string, *args)
except Exception:
print fmt_string, args
raise
def tlv_list(*tlvs):
return ''.join((lambda .0: for t in .0:
tlv(*t))(tlvs))
def s_tlv(data, byte_order = '!'):
(t, l) = struct.unpack(byte_order + 'HH', data[:4])
data = data[4:]
v = data[:l]
data = data[l:]
return (util.Storage(t = t, l = l, v = v), data)
def tlv_list_to_dict(tlv_list):
return odict((lambda .0: for tlv in .0:
(tlv.t, tlv.v))(tlv_list))
def s_tflv(data, byte_order = '!'):
fmt = (('type', 'H'), ('flags', 'B'), ('length', 'B'), ('value', 's', 'length'))
(t, f, l, v, data) = apply_format(fmt, data)
return (util.Storage(t = t, f = f, l = l, v = v), data)
def list_reader(of_what, byte_order = '!'):
def read_list(data, count = (None, -1)):
objs = []
while data and count:
(thing, data) = of_what(data, byte_order)
objs.append(thing)
try:
count -= 1
continue
except TypeError:
print count
raise
continue
None<EXCEPTION MATCH>TypeError
return (objs, data)
return read_list
s_tflv_list = list_reader(s_tflv)
s_tlv_list = list_reader(s_tlv)
def decode(s, enc):
if enc.find('; charset="') != -1:
(msgtype, encoding) = enc.split('; charset="')
encoding = encoding[:-1]
else:
log.warning('oscar.decode encountered "%s", no charset--assuming utf-8', enc)
encoding = 'utf-8'
encoding = encoding.split('\x00', 1)[0]
encodings = {
'unicode-2-0': 'utf-16be',
'utf-8': 'utf-8',
'us-ascii': 'ascii' }
encoding = encodings.get(encoding, encoding)
return util.fuzzydecode(s, encoding)
struct_types = set('xcbhilqfdsp')
oscar_types = set([
'tlv',
'tlv_list',
'tlv_dict',
'named_tlvs',
'list',
'tflv',
'tflv_list',
'rate_class_list',
'rate_group_list',
'pstring',
'message_block',
'ssi_dict',
'ssi',
'tlv_list_len',
'tlv_dict_len',
'userinfo',
'dc_info',
'rate_class',
'msg_fragment',
'lnts'])
all_format_types = struct_types | oscar_types
digits = set(string.digits)
def apply_format(format, data, byte_order = '!'):
if not isinstance(format, (tuple, list)) and isinstance(format[0], (tuple, list)):
raise TypeError('apply_format needs a tuple of tuples')
fields = { }
to_return = []
for item in format:
name = item[0]
kind = item[1]
args = item[2:]
skind = kind
while len(skind) > 0 and skind[0] in digits:
skind = skind[1:]
if kind.lower() in oscar_types:
f = globals().get('apply_format_%s' % kind, None)
if f is None:
raise Exception('%r is not a valid format type' % kind)
(fields[name], data) = f(data, fields, byte_order, *args)
to_return.append(fields[name])
continue
prev_name = None if args else None
if prev_name is not None:
fmt = None % ('%d%s' if isinstance(prev_name, int) else fields[prev_name], kind)
else:
fmt = kind
fmt = byte_order + fmt
try:
sz = struct.calcsize(fmt)
except Exception:
print 'BAD FORMAT:', fmt
raise
try:
fields[name] = struct.unpack(fmt, data[:sz])
except Exception:
print name, fmt, util.to_hex(data)
raise
if len(fields[name]) == 1:
fields[name] = fields[name][0]
data = data[sz:]
to_return.append(fields[name])
to_return.append(data)
return to_return
def apply_format_tlv(data, fields, byte_order):
return s_tlv(data)
def apply_format_tlv_dict(data, fields, byte_order, num_tlvs_s = -1):
(tlvs, data) = apply_format_tlv_list(data, fields, byte_order, num_tlvs_s)
return (tlv_list_to_dict(tlvs), data)
def apply_format_tlv_dict_len(data, fields, byte_order, byte_count_s):
(tlvs, data) = apply_format_tlv_list_len(data, fields, byte_order, byte_count_s)
return (tlv_list_to_dict(tlvs), data)
def apply_format_tlv_list(data, fields, byte_order, num_tlvs_s = -1):
if isinstance(num_tlvs_s, basestring):
num_tlvs = fields[num_tlvs_s]
else:
num_tlvs = num_tlvs_s
return s_tlv_list(data, num_tlvs)
def apply_format_tlv_list_len(data, fields, byte_order, byte_count_s):
if isinstance(byte_count_s, basestring):
byte_count = fields[byte_count_s]
else:
byte_count = byte_count_s
indata = data[:byte_count]
rdata = data[byte_count:]
(tlvs, outdata) = apply_format_tlv_list(indata, fields, byte_order, -1)
return (tlvs, rdata)
def apply_format_named_tlvs(data, fields, byte_order, tlv_count_s, tlv_types):
if isinstance(tlv_count_s, basestring):
tlv_count = fields[tlv_count_s]
else:
tlv_count = tlv_count_s
(tlvs, data) = s_tlv_list(data, tlv_count)
bynumber = tlv_list_to_dict(tlvs)
named_tlvs = util.Storage()
for type, tlv in bynumber.iteritems():
named_tlvs[tlv_types.get(type, type)] = tlv
return (named_tlvs, data)
def apply_format_tflv(data, fields, byte_order):
return s_tflv(data)
def apply_format_tflv_list(data, fields, byte_order, num_tflvs_s = -1):
if isinstance(num_tflvs_s, basestring):
num_tflvs = fields[num_tflvs_s]
else:
num_tflvs = num_tflvs_s
return s_tflv_list(data, num_tflvs)
def apply_format_userinfo(data, fields, byte_order):
userinfo_types = {
1: 'user_class',
2: 'create_time',
3: 'signon_time',
4: 'idle_time',
5: 'account_creation_time',
6: 'user_status_icq',
10: 'external_ip_icq',
12: 'dc_info',
13: 'capabilities',
15: 'online_time',
29: 'avail_msg',
35: 'profile_updated',
38: 'mystery_updated',
39: 'away_updated' }
fmt = (('sn', 'pstring'), ('warning', 'H'), ('num_tlvs', 'H'), ('tlvs', 'named_tlvs', 'num_tlvs', userinfo_types))
(sn, warning, __, userdict, data) = apply_format(fmt, data)
userdict.nice_name = sn
userdict.name = sn.lower().replace(' ', '')
userdict.warning_level = warning
return (userdict, data)
def apply_format_dc_info(data, fields, byte_order):
names = 'ip port type version cookie web_port features info_update_time '.split()
sizes = '4B I B H I I I I '.split()
names += 'ext_update_time status_update_time unknown'.split()
sizes += 'I I H '.split()
stuff = apply_format(zip(names, sizes), data)
return (util.Storage(zip(names, stuff[:-1])), data[-1])
def apply_format_list(data, fields, byte_order, kind, count_s = -1):
if isinstance(count_s, basestring):
count = fields[count_s]
else:
count = count_s
result = []
while data and count:
(info, data) = apply_format((('info', kind),), data, byte_order)
result.append(info)
count -= 1
return (result, data)
def apply_format_rate_class(data, fields, byte_order):
fmt_str1 = 'id H window I clear_level I alert_level I limit_level I disconnect_level I current_level I max_level I last_time I state B'.split()
rate_class_fmt = zip(fmt_str1[::2], fmt_str1[1::2])
info = apply_format(rate_class_fmt, data, byte_order)
data = info.pop()
rate_class = util.Storage(zip(fmt_str1[::2], info))
return (rate_class, data)
def apply_format_rate_class_list(data, fields, byte_order, num_classes_s):
if isinstance(num_classes_s, basestring):
num_classes = fields[num_classes_s]
else:
num_classes = num_classes_s
classes = []
while data and num_classes:
(rate_class, data) = apply_format_rate_class(data, fields, byte_order)
classes.append(rate_class)
num_classes -= 1
return (classes, data)
def apply_format_pstring(data, fields, byte_order):
l = struct.unpack('B', data[:1])[0]
string_ = struct.unpack(str(l) + 's', data[1:1 + l])[0]
return (string_, data[l + 1:])
def apply_format_message_block(data, fields, byte_order):
msg = util.Storage()
fmt = (('msg_fragments', 'list', 'msg_fragment'),)
(msg_fragments, data) = apply_format(fmt, data)
msgs = _[1]
res = dict(msg_fragments)
return (res, data)
def apply_format_msg_fragment(data, fields, byte_order):
fmt = (('type', 'B'), ('version', 'B'), ('length', 'H'), ('data', 's', 'length'))
(type, __, __, data, remaining_data) = apply_format(fmt, data, byte_order)
return ((type, data), remaining_data)
def apply_format_rate_group(data, fields, byte_order):
_data = data[:]
rate_group_fmt = (('id', 'H'), ('num_pairs', 'H'), ('pairs_list', 'list', 'I', 'num_pairs'))
try:
stuff = apply_format(rate_group_fmt, data, byte_order)
except ValueError:
e = None
stuff = apply_format(rate_group_fmt, _data, '<')
(id, num_pairs, pairs_list, data) = stuff
pairs = []
for pair in pairs_list:
(fam, sub) = struct.unpack(byte_order + 'HH', struct.pack(byte_order + 'I', pair))
pairs.append((fam, sub))
return (dict.fromkeys(pairs, id), data)
def apply_format_rate_group_list(data, fields, byte_order, num_groups_s):
if isinstance(num_groups_s, basestring):
num_groups = fields[num_groups_s]
else:
num_groups = num_groups_s
rate_groups = { }
while data and num_groups:
(rate_group, data) = apply_format_rate_group(data, fields, byte_order)
rate_groups.update(rate_group)
num_groups -= 1
return (rate_groups, data)
def apply_format_ssi(data, fields, byte_order):
ssi_fmt = (('name_len', 'H'), ('name', 's', 'name_len'), ('group_id', 'H'), ('item_id', 'H'), ('type_', 'H'), ('data_len', 'H'), ('tlvs', 'tlv_dict_len', 'data_len'))
(__, name, group_id, item_id, type_, __, tlvs, data) = apply_format(ssi_fmt, data, byte_order)
return (oscar.ssi.item(name, group_id, item_id, type_, tlvs), data)
def apply_format_ssi_list(data, fields, byte_order, num_ssis_s = -1):
if isinstance(num_ssis_s, basestring):
num_ssis = fields[num_ssis_s]
else:
num_ssis = num_ssis_s
l = []
while data and num_ssis != 0:
(ssi, data) = apply_format_ssi(data, fields, byte_order)
l.append(ssi)
num_ssis -= 1
return (l, data)
def apply_format_ssi_dict(*args):
(l, data) = apply_format_ssi_list(*args)
d = { }
for item in l:
d[(item.group_id, item.item_id)] = item
return (d, data)
def apply_format_lnts(data, fields, byte_order = '<'):
if byte_order != '<':
import warnings as warnings
warnings.warn('oscar.apply_format_lnts got a byte order other than little-endian')
length = struct.unpack(byte_order + 'H', data[:2])[0]
data = data[2:]
val = data[:length]
data = data[length:]
if length:
pass
return (val[:-1], data)
def chat_cookie(room_name, exchange = 4):
return '!aol://2719:10-' + str(exchange) + '-' + str(room_name.replace(' ', '').lower())